#!/usr/bin/python
# -*- coding: utf-8 -*-

import unittest
from interface02.common.readExcle import *
import requests
from ddt import ddt, data, unpack
from interface02.common.configHttp import *
from interface02.common.writeExcle import *


#调用实例方法获取测试数据
data_obj = ReadExcle()
test_data = data_obj.read()

#创建用例，定义一个测试类
#使用ddt装饰器，对每条用例进行调用
@ddt
class TestCase(unittest.TestCase):
    #创建测试用例方法
    @data(*test_data)
    @unpack
    def test_case(self, id, interfaceUrl, name, method, value, expect, real, status):
        # url = test_data[0]['interfaceUrl']
        # method = test_data[0]['method']
        # value = test_data[0]['value']
        # expect = test_data[0]['expect']
        #
        # #进行request请求，获取返回结果
        # if method == 'get':
        #     res = requests.get(url=url, params=value)
        # elif method == 'post':
        #     res = requests.post(url=url, data=value)

        value = eval(value)
        expect = int(expect)
        # 实例化confighttp类，进行调用
        res = ConfigHttp(interfaceUrl, method, value)
        re = res.run()

        #转换接口返回格式
        res_dict = re.json()
        real = res_dict['errorCode']
        try:
            if real == expect:
                status = 'pass'
            else:
                status = 'fail'
            w = WriteExcle()
            w.write_data(int(id), 6, real, status)
        except Exception as msg:
            print(f'报错了：{msg}')
            raise

    #接口断言
        self.assertEqual(real, expect, '测试用例不通过！')


if __name__ == '__main__':
    unittest.main(verbosity=2)

